package com.alvis.exam.service.impl;

import com.alvis.exam.domain.TClass;
import com.alvis.exam.domain.TStudent;
import com.alvis.exam.domain.TStudentExample;
import com.alvis.exam.domain.TStudentExample.Criteria;
import com.alvis.exam.domain.User;
import com.alvis.exam.repository.TStudentMapper;
import com.alvis.exam.repository.UserMapper;
import com.alvis.exam.service.AuthenticationService;
import com.alvis.exam.service.StudentService;
import com.alvis.exam.service.impl.BaseServiceImpl;
import com.alvis.exam.viewmodel.admin.administration.StudentPageRequestVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends BaseServiceImpl<TStudent> implements StudentService {

    private TStudentMapper mapper;
    private UserMapper userMapper;
    private AuthenticationService authenticationService;

    @Autowired
    private StudentServiceImpl(TStudentMapper mapper, UserMapper userMapper,AuthenticationService authenticationService) {
        super(mapper);
        this.mapper = mapper;
        this.userMapper = userMapper;
        this.authenticationService = authenticationService;
    }

    public List<TStudent> getAll() {
        TStudentExample example = new TStudentExample();
        example.orderBy("add_time asc");
        return mapper.selectByExample(example);
    }

    public List<TStudent> querySelective(StudentPageRequestVM model) {
        TStudentExample example = new TStudentExample();
        if (StringUtil.isNotEmpty(model.getKeyWord())) {
            Criteria snoCriteria = example.createCriteria();
            snoCriteria.andSnoLike("%" + model.getKeyWord() + "%");

            Criteria departmentCriteria = example.createCriteria();
            departmentCriteria.andDepartmentLike("%" + model.getKeyWord() + "%");

            Criteria majorCriteria = example.createCriteria();
            majorCriteria.andMajorLike("%" + model.getKeyWord() + "%");

            Criteria classNameCriteria = example.createCriteria();
            classNameCriteria.andClassNameLike("%" + model.getKeyWord() + "%");

            Criteria nameCriteria = example.createCriteria();
            nameCriteria.andNameLike("%" + model.getKeyWord() + "%");

            example.or(snoCriteria);
            example.or(departmentCriteria);
            example.or(majorCriteria);
            example.or(classNameCriteria);
            example.or(nameCriteria);
        } else {
            Criteria criteria = example.createCriteria();
            criteria.when(StringUtils.isNoneBlank(model.getSno()),criteria1 -> criteria.andSnoLike("%" + model.getSno() +"%"))
                    .when(StringUtils.isNoneBlank(model.getDepartment()), criteria1 -> criteria.andDepartmentLike("%" + model.getDepartment() + "%"))
                    .when(StringUtils.isNoneBlank(model.getMajor()), criteria1 -> criteria.andMajorLike("%" + model.getMajor() + "%"))
                    .when(StringUtils.isNoneBlank(model.getClassName()), criteria1 -> criteria.andClassNameLike("%" + model.getClassName() + "%"))
                    .when(StringUtils.isNoneBlank(model.getName()), criteria1 -> criteria.andNameLike("%" + model.getName() + "%"));
        }
        PageHelper.startPage(model.getPageIndex(), model.getPageSize());
        return mapper.selectByExample(example);
    }

    public void saveOrUpdateBatch(List<TStudent> list) {
        mapper.insertOrUpdateBatch(list);
        TStudentExample example = new TStudentExample();
        userMapper.insertOrUpdateBatch(mapper.selectByExample(example).stream().map((student -> {
            User user = new User();
            user.setId(student.getId());
            user.setSno(student.getId());
            user.setUserName(user.getId().toString());
            user.setPassword(authenticationService.pwdEncode(student.getId().toString()));

            user.setRealName(student.getName());

            if (student.getSex().equals("男")) {
                user.setSex(1);
            } else if (student.getSex().equals("女")) {
                user.setSex(2);
            } else {
                user.setSex(0);
            }

            user.setRole(1);

            user.setStatus(1);

            user.setCreateTime(LocalDateTime.now());

            user.setModifyTime(LocalDateTime.now());

            user.setDeleted(false);

            return user;
        })).collect(Collectors.toList()));
    }

    public int count() {
        TStudentExample example = new TStudentExample();
        return (int) mapper.countByExample(example);
    }

    public List<TClass> getAllClass() {
        return mapper.getAllClass();
    }

    @Transactional
    public void deleteAll() {
        TStudentExample example = new TStudentExample();
        mapper.deleteByExample(example);
    }
}
